
package com.be.beadmin.device.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.device.entity.EhsItem;
import com.be.beadmin.device.entity.EhsItem_;
import com.be.beadmin.device.entity.EhsStandard;
import com.be.beadmin.device.service.EhsStandardService;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.EhsItemMapperEx;
import com.be.beadmin.device.repository.EhsItemRepository;
import com.be.beadmin.device.service.EhsItemService;
import com.be.beadmin.device.dto.input.EhsItemDto;
import com.be.beadmin.device.dto.output.EhsItemEx;
import com.be.beadmin.device.mapper.EhsItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zty
* @date 2023-08-13
**/
@Service
@RequiredArgsConstructor
public class EhsItemServiceImpl implements EhsItemService {

    @Autowired
    private  EhsItemRepository ehsItemRepository;
    @Autowired
    private  EhsItemMapper ehsItemMapper;
    @Autowired
    private  EhsItemMapperEx ehsItemExMapper;
    @Autowired
    private EhsStandardService ehsStandardService;
    @Override
    public Page<EhsItemEx> getEhsItemByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<EhsItem> page = ehsItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(EhsItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(EhsItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<EhsItemEx> page1 = page.map(ehsItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addEhsItem(EhsItemDto ehsItemDto) {
            EhsItem result = ehsItemRepository.findEhsItemByIdAndDelete(ehsItemDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("EhsItem不存在,请重新查询");
            }
            //保证每个ehs巡检单中的检查项目的唯一性
            result = ehsItemRepository.findEhsItemByCheckIdAndItemNameAndDelete(ehsItemDto.getCheckId() , ehsItemDto.getItemName() , 0);
            if(ObjectUtil.isNotEmpty(result)){
                throw new BeadminException("检查项目重复，请重新输入");
            }
            EhsItem ehsItem = ehsItemMapper.toEntity(ehsItemDto);
            ehsItemRepository.save(ehsItem);
    }

    @Override
    public void updateEhsItem(EhsItemDto ehsItemDto) {
            EhsItem ehsItem = ehsItemRepository.findEhsItemById(ehsItemDto.getId());
            ehsItem = ehsItemMapper.toEntity(ehsItemDto);
            ehsItemRepository.save(ehsItem);
    }


    @Override
    public void delEhsItem(String id) {
        EhsItem ehsItem = ehsItemRepository.findEhsItemById(id);
        ehsItem.setDelete(1);
        ehsItemRepository.save(ehsItem);
    }

    @Override
    public void delEhsItems (List<String> ids) { }

    @Override
    public EhsItem receiveEhsItem(String templateId) {
        EhsStandard ehsStandard = ehsStandardService.getEhsStandardByTemplateID(templateId);
        EhsItem ehsItem = new EhsItem();
        ehsItem.setItemDesc(ehsStandard.getCheckDesc());
        ehsItem.setTemplateId(ehsStandard.getTemplateId());
        ehsItem.setItemName(ehsStandard.getCheckItem());
        ehsItem.setIsAttached(ehsStandard.getIsAttached());
        ehsItem.setNotes(ehsStandard.getNotes());
        return ehsItem;
    }
}